package managedbean.stakeholder;

import entity.PortalDescriptionLocale;
import entity.StakeholderEntity;
import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpSession;
import session.stateless.StakeholderSessionBeanStateless;
import util.entityhelper.StakeholderAccountState;

/**
 *
 * @author jamie
 * 
 */
@ManagedBean
@ViewScoped
public class StakeholderLocaleManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBeanStateless stakeholderPortalMgt;
    private Long stakeholderId;
    private StakeholderAccountState stakeholderAccountState;
    private String nextPage;
    private String chineseLocale = "zh_CN";
    private String malayLocale = "ms_MY";
   // private String engSG=""
    private String mBizName;
    private String cBizName;
    private String cBizDesc;
    private String mBizDesc;
    private int sNo;
    private List<PortalDescriptionLocale> tempLocale;
    private StakeholderEntity stakeholder;
    private FacesContext ctx = FacesContext.getCurrentInstance();

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        HttpSession session = (HttpSession) getCtx().getExternalContext().getSession(true);

        stakeholderId = ((Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId"));

        if (getStakeholderId() == null) {

            System.out.println("enter stakeholderID = null? ");
            setNextPage(getCtx().getExternalContext().encodeActionURL(getCtx().getApplication().getViewHandler().getActionURL(getCtx(), "/StakeholderLogin.xhtml")));
            try {
                getCtx().getExternalContext().redirect(getNextPage());
            } catch (IOException ioe) {
                ioe.printStackTrace();

            }
        } else {
            if (getStakeholderAccountState() == null) {


                try {

                    stakeholderAccountState = stakeholderPortalMgt.getStakeholderPortalInfoById(getStakeholderId());

                    System.out.println("userId at locale " + getStakeholderId());

                    stakeholder = stakeholderPortalMgt.retrieveStakeholderInfoByUsername(stakeholderAccountState.getUsername());

                    stakeholderAccountState.setLocaleDesc(stakeholderPortalMgt.getStakeholderLocaleDetail(stakeholderId));

                    generateDefaultValue();
                    loadDatabaseValue();

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    public void saveStakeholderPortalInfo(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        boolean success = false;

        System.out.println("stakeholder locale saved!: " + stakeholderAccountState.getStakeholderDesc());
        String statusMessage = null;
        try {
            
            setValuetoList();
            
            success = stakeholderPortalMgt.addLocalePortal(stakeholderAccountState);
            FacesMessage msg = new FacesMessage("Succesfully saved! ");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            System.out.println("success at localse" + success);
        } catch (ExistException ex) {
            ex.printStackTrace();
        }
    }

    private void generateDefaultValue() {

        //set to default value; 
        sNo =stakeholderAccountState.getLocaleDesc().size();
                

        mBizName = stakeholderAccountState.getCorpName();
        cBizName = stakeholderAccountState.getCorpName();

        mBizDesc = stakeholderAccountState.getStakeholderDesc();
        cBizDesc = stakeholderAccountState.getStakeholderDesc();

        //info.setstakeholderAccountState.getId();

    }

    private void loadDatabaseValue() {

        //load into respective value
        int size = stakeholderAccountState.getLocaleDesc().size();

        PortalDescriptionLocale localeEntity = new PortalDescriptionLocale();
        for (int i = 0; i < size; i++) {
            //do comparsion
            localeEntity = stakeholderAccountState.getLocaleDesc().get(i);
            if (localeEntity.getLocale().equals(malayLocale)) {
                mBizName = localeEntity.getBizName();
                mBizDesc = localeEntity.getStakeholderDesc();
            } else if (localeEntity.getLocale().equals(chineseLocale)) {
                cBizName = localeEntity.getBizName();
                cBizDesc = localeEntity.getStakeholderDesc();
            }


        }
    }
    

    private void setValuetoList() {

        PortalDescriptionLocale locale = new PortalDescriptionLocale();
        tempLocale = new ArrayList<PortalDescriptionLocale>();
        // add in malay localse
        locale.setLocale(malayLocale);
        locale.setBizName(mBizName);
        locale.setStakeholderDesc(mBizDesc);
        tempLocale.add(locale);
        System.out.println("Locale 1: " + locale.getLocale() + " " + locale.getBizName() + " " + locale.getStakeholderDesc());
        
        PortalDescriptionLocale locale2 = new PortalDescriptionLocale();
        
        //too add chinese localse
        locale2.setLocale(chineseLocale);
        locale2.setBizName(cBizName);
        locale2.setStakeholderDesc(cBizDesc);
        tempLocale.add(locale2);
        System.out.println("Locale 2: " + locale2.getLocale() + " " + locale2.getBizName() + " " + locale2.getStakeholderDesc());
        
        //add into local which will be added into db
        
        stakeholderAccountState.setLocaleDesc(tempLocale);
        for(int i =0; i< stakeholderAccountState.getLocaleDesc().size(); i++){
        System.out.println("Local List: " + stakeholderAccountState.getLocaleDesc().get(i).getBizName());
        System.out.println("lalala: "+ stakeholderAccountState.getLocaleDesc().get(i).getLocale());
                }
    }

    /**
     * @return the stakeholderPortalMgt
     */
    public StakeholderSessionBeanStateless getStakeholderPortalMgt() {
        return stakeholderPortalMgt;
    }

    /**
     * @param stakeholderPortalMgt the stakeholderPortalMgt to set
     */
    public void setStakeholderPortalMgt(StakeholderSessionBeanStateless stakeholderPortalMgt) {
        this.stakeholderPortalMgt = stakeholderPortalMgt;
    }

    /**
     * @return the stakeholderId
     */
    public Long getStakeholderId() {
        return stakeholderId;
    }

    /**
     * @param stakeholderId the stakeholderId to set
     */
    public void setStakeholderId(Long stakeholderId) {
        this.stakeholderId = stakeholderId;
    }

    /**
     * @return the stakeholderAccountState
     */
    public StakeholderAccountState getStakeholderAccountState() {
        return stakeholderAccountState;
    }

    /**
     * @param stakeholderAccountState the stakeholderAccountState to set
     */
    public void setStakeholderAccountState(StakeholderAccountState stakeholderAccountState) {
        this.stakeholderAccountState = stakeholderAccountState;
    }

    /**
     * @return the nextPage
     */
    public String getNextPage() {
        return nextPage;
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the ctx
     */
    public FacesContext getCtx() {
        return ctx;
    }

    /**
     * @param ctx the ctx to set
     */
    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    /**
     * @return the tempLocale
     */
    public List<PortalDescriptionLocale> getTempLocale() {
        return tempLocale;
    }

    /**
     * @param tempLocale the tempLocale to set
     */
    public void setTempLocale(List<PortalDescriptionLocale> tempLocale) {
        this.tempLocale = tempLocale;
    }

    /**
     * @return the chineseLocale
     */
    public String getChineseLocale() {
        return chineseLocale;
    }

    /**
     * @param chineseLocale the chineseLocale to set
     */
    public void setChineseLocale(String chineseLocale) {
        this.chineseLocale = chineseLocale;
    }

    /**
     * @return the malayLocale
     */
    public String getMalayLocale() {
        return malayLocale;
    }

    /**
     * @param malayLocale the malayLocale to set
     */
    public void setMalayLocale(String malayLocale) {
        this.malayLocale = malayLocale;
    }

    /**
     * @return the mBizName
     */
    public String getmBizName() {
        return mBizName;
    }

    /**
     * @param mBizName the mBizName to set
     */
    public void setmBizName(String mBizName) {
        this.mBizName = mBizName;
    }

    /**
     * @return the cBizName
     */
    public String getcBizName() {
        return cBizName;
    }

    /**
     * @param cBizName the cBizName to set
     */
    public void setcBizName(String cBizName) {
        this.cBizName = cBizName;
    }

    /**
     * @return the cBizDesc
     */
    public String getcBizDesc() {
        return cBizDesc;
    }

    /**
     * @param cBizDesc the cBizDesc to set
     */
    public void setcBizDesc(String cBizDesc) {
        this.cBizDesc = cBizDesc;
    }

    /**
     * @return the mBizDesc
     */
    public String getmBizDesc() {
        return mBizDesc;
    }

    /**
     * @param mBizDesc the mBizDesc to set
     */
    public void setmBizDesc(String mBizDesc) {
        this.mBizDesc = mBizDesc;
    }

    /**
     * @return the stakeholder
     */
    public StakeholderEntity getStakeholder() {
        return stakeholder;
    }

    /**
     * @return the sNo
     */
    public int getsNo() {
        return sNo;
    }

    /**
     * @param sNo the sNo to set
     */
    public void setsNo(int sNo) {
        this.sNo = sNo;
    }
}
